home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gcctest / tests05.zoo / tqsort.c < prev    next >
C/C++ Source or Header  |  1992-03-28  |  3KB  |  156 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/times.h>
  4. #include <sys/param.h>
  5.  
  6.  
  7. #ifndef HZ
  8. #define HZ 60
  9. #endif
  10.  
  11. #ifdef atarist
  12. long _stksize =-1L;
  13. #endif
  14.  
  15. #ifdef __STDC__
  16. void             *malloc(unsigned long n);
  17. long             random(void);
  18. long            atol(char *);
  19. #else
  20. #define void char
  21. void             *malloc();
  22. long             random();
  23. long            atol();
  24. #endif
  25.  
  26. #ifdef __hpux
  27. #define random rand
  28. #endif
  29.  
  30. int compare(p1,  p2)
  31. long *p1, *p2;
  32. {
  33.     return (int)(*p1 - *p2);
  34. }
  35.  
  36. int dcompare(p1,  p2)
  37. double *p1, *p2;
  38. {
  39.     double q1 = *p1, q2 = *p2;
  40.  
  41.     if(q1 < q2)
  42.         return -1;
  43.     else if(q1 == q2)
  44.         return 0;
  45.     else
  46.         return 1;
  47. }
  48.  
  49. long tcase[] = {101, 100, 99, 3, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  50.            1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -99, -100, -101};
  51. long ntcase = sizeof(tcase)/sizeof(long);
  52.  
  53. int main(argc, argv)
  54. int argc;
  55. char **argv;
  56. {
  57.     register long i;
  58.     register long siz;
  59.     register unsigned long start, tot, grand;
  60.     long *a;
  61.     double *d;
  62.     struct tms t;
  63.     
  64.     siz = (argc > 1)? atol(*++argv) : 10000;
  65.     if(( a = (long *)malloc((unsigned long)(siz * sizeof(long)))) == (long *)NULL)
  66.     {
  67.         fprintf(stderr,"No mem\n");
  68.         exit(1);
  69.     }
  70. /*     printf("%ld elems\n", siz); */
  71.     
  72.     /* case  1*/
  73.     for(i = 0 ; i < siz; i++) a[i] = siz-i;
  74.     times(&t);
  75.     start = t.tms_utime;
  76.     qsort((char *)a, siz, sizeof(long), compare);
  77.     times(&t);
  78.     tot = (t.tms_utime - start);
  79.     for(i = 1; i < siz; i++)
  80.     {
  81.         if(a[i-1] > a[i])
  82.         {
  83.             fprintf(stderr,"Case 1: Not sorted at %ld (%ld  %ld)\n",
  84.                  i, a[i-1], a[i]);
  85.             return(2);
  86.         }
  87.     }
  88. /*    printf("case 1 %ld secs (%ld)\n", tot/HZ, tot); */
  89.     grand = tot;
  90.  
  91.     /* random */
  92.     for(i = 0; i < siz; i++) a[i] =random() ;
  93.     times(&t);
  94.     start = t.tms_utime;
  95.     qsort((char *)a, siz, sizeof(long), compare);
  96.     times(&t);
  97.     tot = (t.tms_utime - start);
  98.     for(i = 1; i < siz; i++)
  99.     {
  100.         if(a[i-1] > a[i])
  101.         {
  102.             fprintf(stderr,"Random: Not sorted at %ld (%ld  %ld)\n",
  103.                  i, a[i-1], a[i]);
  104.             return(3);
  105.         }
  106.     }
  107. /*    printf("Rand  %ld secs (%ld)\n", tot/HZ, tot); */
  108.     grand += tot;
  109.     free(a);
  110.  
  111.     /* tcase */
  112.     times(&t);
  113.     start = t.tms_utime;
  114.     qsort((char *)tcase, ntcase, sizeof(long), compare);
  115.     times(&t);
  116.     tot = (t.tms_utime - start);
  117.     for(i = 1; i < ntcase; i++)
  118.     {
  119.         if(tcase[i-1] > tcase[i])
  120.         {
  121.             fprintf(stderr,"Tcase: Not sorted at %ld (%ld  %ld)\n",
  122.                  i, tcase[i-1], tcase[i]);
  123.             exit(3);
  124.         }
  125.     }
  126. /*    printf("Tcase  %ld secs (%ld)\n", tot/HZ, tot); */
  127.     grand += tot;
  128.  
  129.     /* double */
  130.     if(( d = (double *)malloc((unsigned long)(siz * sizeof(double)))) == (double *)NULL)
  131.     {
  132.         fprintf(stderr,"No mem\n");
  133.         exit(1);
  134.     }
  135.     for(i = 0; i < siz; i++) d[i] =random() ;
  136.     times(&t);
  137.     start = t.tms_utime;
  138.     qsort((char *)d, siz, sizeof(double), dcompare);
  139.     times(&t);
  140.     tot = (t.tms_utime - start);
  141.     for(i = 1; i < siz; i++)
  142.     {
  143.         if(d[i-1] > d[i])
  144.         {
  145.             fprintf(stderr,"Double: Not sorted at %ld (%f  %f)\n",
  146.                  i, d[i-1], d[i]);
  147.             exit(3);
  148.         }
  149.     }
  150. /*    printf("Double  %ld secs (%ld)\n", tot/HZ, tot); */
  151.     grand += tot;
  152.  
  153.     printf("Total %ld secs (%ld)\n", grand/HZ, grand);
  154.         return 0;
  155. }
  156.